package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/alert-rule")
@Slf4j
@Api(tags = "报警规则管理")
public class AlertRuleController {
    @Autowired
    private AlertRuleService alertRuleService;

    @PostMapping("/create")
    @ApiOperation(value = "新增报警规则", notes = "新增报警规则")
    public ResponseResult createAlertRule(@RequestBody AlertRuleDto alertRuleDto) {
        alertRuleService.createAlertRule(alertRuleDto);
        return ResponseResult.success();
    }

    @GetMapping("/get-page")
    @ApiOperation(value = "分页获取告警规则列表", notes = "分页获取告警规则列表")
    public ResponseResult<PageResponse<AlertRuleVo>> getPageAlertRule(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "alertRuleName", required = false) String alertRuleName,
            @RequestParam(value = "functionName", required = false) String functionName,
            @RequestParam(value = "productKey", required = false) String productKey
    ) {
        PageResponse<AlertRuleVo> alertRuleVoPageResponse = alertRuleService.getPageAlertRule(pageNum, pageSize, alertRuleName, functionName, productKey);
        return ResponseResult.success(alertRuleVoPageResponse);
    }

    @GetMapping("/read/{id}")
    @ApiOperation(value = "获取单个告警规则", notes = "获取单个告警规则")
    public ResponseResult<AlertRuleVo> readById(@PathVariable Long id) {
        AlertRuleVo alertRuleVo = alertRuleService.readById(id);
        return ResponseResult.success(alertRuleVo);
    }

    @PutMapping("/update/{id}")
    @ApiOperation(value = "更新告警规则", notes = "更新告警规则")
    public ResponseResult updateById(@RequestBody AlertRuleDto alertRuleDto, @PathVariable Long id) {
        alertRuleService.updateById(alertRuleDto);
        return ResponseResult.success();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除告警规则", notes = "删除告警规则")
    public ResponseResult deleteById(@PathVariable long id) {
        alertRuleService.deleteById(id);
        return ResponseResult.success();
    }

    @PutMapping("/{id}/status/{status}")
    @ApiOperation(value = "启用禁用",notes = "启用禁用")
    public ResponseResult okNo(@PathVariable long id,@PathVariable Integer status){
        alertRuleService.okNo(id,status);
        return ResponseResult.success();
    }

}
